//
// 2.10.1 use select() function to implement a portable sleep() function.
//
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

void sleep_seconds(int nSeconds)
{
    struct timeval tv;
    tv.tv_sec = nSeconds;
    tv.tv_usec = 0;
    select(0, NULL, NULL, NULL, &tv);
}

void sleep_milliseconds(int ms)
{
    struct timeval tv;
    tv.tv_sec = ms / 1000;
    tv.tv_usec = ms % 1000 * 1000;
    select(0, NULL, NULL, NULL, &tv);
}

int main(int argc, char** argv)
{
    printf("Test sleeping, implement by select.\n");
    printf("sleep 3 seconds:\n");
    sleep_seconds(3);
    printf("sleep 1500 milliseconds:\n");
    sleep_milliseconds(1500);
    printf("done!\n");
    return 0;
}
